<?php  
namespace App\Libs\Helper;
//图片生成：主要是用在生成证书之类的。
class ImageGenerate{
	protected $img = '';//源图片，做为背景
	protected $imgType = '';//图片类型
	
	
	public function __construct( $img ){
		$imgInfo = pathinfo($img);
		if( $imgInfo['extension'] == 'jpg' || $imgInfo['extension'] == 'jpeg' ){
			$this->img = imagecreatefromjpeg( $img );
			$this->imgType = 'jpeg';
		}else if( $imgInfo['extension'] == 'png' ){
			$this->img = imagecreatefrompng ( $img );
			$this->imgType = 'png';
		}else{
			return false;
		}
		$this->red = imagecolorallocate($this->img,00,00,00);                 // 字体颜色
		$this->grey = imagecolorallocate($this->img, 128,128,128);
	}
	
	/**
	*	添加文字
	*		fontSize:字体大小
	*		angle:角度制表示的角度
	*		x:x坐标轴
	*		y:y坐标轴
	*		txt:展示的文字
	*		font:字体
	*/
	public function imageText( $fontSize = 35, $angle = 0 , $x = 20, $y = 20 , $txt = '测试' , $color = 'grey' , $font = './fzltzh.TTF' ){
		imageTTFText($this->img, $fontSize , $angle , $x , $y , $this->$color, $font ,$txt);
	}
	
	/**
	*	合成另一张图
	*		path : 图片路径
	*		x:x坐标轴
	*		y:y坐标轴
	*/
	public function imgMerge( $path , $x = '470' , $y = '60'){
		$image_2 = imagecreatefrompng($path);
		imagecopymerge($this->img,$image_2, $x,$y,0,0,imagesx($image_2),imagesy($image_2), 100);
	}
	
	/**
	*	浏览器展示
	*/
	public function webShow(){
		 header("Content-type: image/png;");
		ImagePng($this->img);
		imagedestroy($this->img);
	}
	
	/**
	*	保存
	*/
	public function saveImg( $path , $name = '' ){
		if( empty( $name ) ){
			 $sid = date("YmdHis").mt_rand(1000, 9999);
			 $name = $sid;
		}
		$filename = $path.$name.'.'.$this->imgType;//路径名称
		ImagePng($this->img, $filename);
		imagedestroy($this->img);
	}
	
	/**
	*	下载
	*/
	public function downloadImg( $filename = '' ){
		$filename = 'certificate_aadarsh.png';
		ImagePng($this->img,$filename);
		header('Pragma: public');
		header('Cache-Control: public, no-cache');
		header('Content-Type: application/octet-stream');
		header('Content-Length: ' . filesize($filename));
		header('Content-Disposition: attachment; filename="' .
		 basename($this->img) . '"');
		header('Content-Transfer-Encoding: binary');
		readfile($this->img);
		imagedestroy($this->img);
	}
	
}
/*
$img = new imageGenerage( './expand_type1.png' );
//$img->imageText( 35 , 0 , 300 , 430  );
$img->imageText( 35 , 0 , 40 , 90 , '我是李鹏'  );
$img->imgMerge('./erweima.png');
$img->webShow();
//$img->saveImg('./','imageGenerate1');*/
/*
$realname = "姓名";
$schoolname = "学校";  
$idcard = "身份证号"; 

$image = imagecreatefromjpeg ('./1234.jpg');           // 证书模版图片文件的路径 
$red = imagecolorallocate($image,00,00,00);                 // 字体颜色
$grey = imagecolorallocate($image, 128,128,128);

// imageTTFText("Image", "Font Size", "Rotate Text", "Left Position","Top Position", "Font Color", "Font Name", "Text To Print");
imageTTFText($image, 35, 0, 300,430, $grey, './fzltzh.TTF',$realname);
imageTTFText($image, 50, 0, 1000, 1023, $red, './fzltzh.TTF', $schoolname);
imageTTFText($image, 55, 0, 900, 810, $red, './fzltzh.TTF', $idcard);

 /* 直接显示在浏览器 */
/*
 header('Content-type: image/png;');
ImagePng($image);
imagedestroy($image);*/
